package com.cn.project.controller;

import com.cn.common.pojo.ProjectModel;
import com.cn.common.pojo.ResultModel;
import com.cn.project.service.ProjectService;
import io.swagger.annotations.Api;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "产品信息")
@RestController
@RequestMapping("project")
public class ProjectController {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String date= DATE_FORMAT.format(new Date());
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    ProjectService projectService;
    @GetMapping("pForId")
     public ResultModel pForId(int id){
    ResultModel byId = projectService.PForId(id);
    return byId;
}
    @GetMapping("pByType")
    public ResultModel pByType(int type){
        List<ProjectModel> type1 = projectService.PByType(type);
        return ResultModel.Data(type1);

    }
    @GetMapping("pForP")
    public ResultModel pForP(){
        return projectService.PForP();
    }
    @GetMapping("project")
    public ResultModel project(){
        ResultModel project = projectService.project();
        return project;
    }
    @GetMapping("delete")
    public ResultModel delete(int id){
        ResultModel delete = projectService.delete(id);
        rabbitTemplate.convertAndSend("userTopicExchange","leader.hello","{reporter:'大哥好',date:\'"+date+"\',content:'我们刚刚删除了一天产品'}");
        return delete;
    }
    @PostMapping("update")
    public ResultModel update(ProjectModel projectModel){
        ResultModel update = projectService.update(projectModel);
        rabbitTemplate.convertAndSend("userTopicExchange","leader.hello","{reporter:'大哥好',date:\'"+date+"\',content:'我们这哈修改了一条产品信息'}");
        return update;
    }
    @PostMapping(value = "insert")
    public ResultModel insert(ProjectModel projectModel){
        ResultModel insert = projectService.insert(projectModel);
        rabbitTemplate.convertAndSend("userTopicExchange","leader.hello","{reporter:'大哥好',date:\'"+date+"\',content:'这是我们的新产品'}");
        return insert;
    }
    @PostMapping(value = "enable")
    public ResultModel enable(int id){
        ResultModel enable = projectService.enable(id);
        return enable;
    }
    @GetMapping("findById")
    public ResultModel findById(int id){
        ResultModel findById = projectService.findById(id);

        return findById;
    }
    @GetMapping("limit")
    public ResultModel limit(ProjectModel projectModel,int limit,int page){
        projectModel.setPageNum(page);
        projectModel.setPageSize(limit);
        ResultModel resultModel = projectService.limit(projectModel);
        return resultModel;
    }
}
